package com.wangbo.rabbit.controller;

import com.wangbo.rabbit.Constant;
import com.wangbo.rabbit.RabbitMessage;
import com.wangbo.rabbit.RabbitProducer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class SendMQController {
    // 本地消息ID生成器
    private final AtomicInteger MESSAGE_ID = new AtomicInteger(0);

    @Resource
    private RabbitProducer rabbitProducer;

    /**
     * 发送消息
     */
    @GetMapping("/sendMQ/{msg}")
    public String sendMQ(@PathVariable String msg) {
        int messageId = MESSAGE_ID.incrementAndGet();
        // 准备消息
        RabbitMessage<String> rabbitMessage = new RabbitMessage<String>()
                .setMessageId(messageId)
                .setBizType(RabbitMessage.BizType.ADD_USER)
                .setPayload(msg);
        // 发送消息
        rabbitProducer.sendMessage(Constant.DIRECT_EXCHANGE_NAME, Constant.DIRECT_ROUTING_KEY, rabbitMessage);
        return "ok";
    }
}
